package demo;

import com.itcv.MethodTimerVisitor;
import com.itcv.util.FileUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

import java.lang.reflect.Field;

public class UpdateMethod {

    public static void main(String[] args) {
       /* String relative_path = "demo/HelloWorld.class";
        String filepath = FileUtils.getFilePath(relative_path);
        byte[] bytes =  FileUtils.readBytes(filepath);

        ClassReader cr = new ClassReader(bytes);

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

        ClassVisitor cv = new MethodTimerVisitor(Opcodes.ASM9,cw);
        //（4）结合 ClassReader 和 ClassVisitor
        int parsingOptions = ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
        cr.accept(cv, parsingOptions);

        //（5）生成 byte[]
        byte[] bytes2 = cw.toByteArray();

        FileUtils.writeBytes(filepath, bytes2);*/


        HelloWorld instance = new HelloWorld();
        try {
          int num =  instance.add(5,3);
            System.out.println(num);
            // 第二部分，来查看方法运行的时间
            Class<?> clazz = HelloWorld.class;
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field f : declaredFields) {
                String fieldName = f.getName();
                f.setAccessible(true);
                if (fieldName.startsWith("timer")) {
                    Object FieldValue = f.get(null);
                    System.out.println(fieldName + " = " + FieldValue);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
